﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkFlowBLL;

namespace QLCF.Forms.admin
{
    public partial class frmAdmin : Form
    {
        public int permission=0;
        public frmAdmin()
        {
            InitializeComponent();
            permission = CtrAdmin.GetPermission(Program.UserEntity.Permission, Config.Enum.Function.frmAdmin);
            if ((permission & (int)Config.Enum.Permission.Edit) != (int)Config.Enum.Permission.Edit)
                contextMenuStripSetRF.Enabled = false;
            if ((permission & (int)Config.Enum.Permission.Add) != (int)Config.Enum.Permission.Add)
                btnAddAdmin.Enabled = false;
            if ((permission & (int)Config.Enum.Permission.Delete) != (int)Config.Enum.Permission.Delete)
                btnDeleteAdmin.Enabled = false;                  
            bindRole();
        }

        private void bindRole()
        {
            CtrAdmin ctrA = new CtrAdmin();
            var roles = ctrA.GetListRole();
            treeRole.Nodes.Clear();
            int i = 0;
            foreach (var role in roles.Items)
            {
                TreeNode node = new TreeNode(role.RoleName);
                node.Tag = role.RoleID;
                treeRole.Nodes.Add(node);
                if (i++ == 0)
                {
                    treeRole.SelectedNode = treeRole.Nodes[0];
                }
            }
        }

        private int roleId = -1;
        private void treeRole_AfterSelect(object sender, TreeViewEventArgs e)
        {
            roleId = Convert.ToInt32(e.Node.Tag);
            LoadRoleFunc();
            LoadAdmin();
            e.Node.BackColor = Color.FromArgb(220, 220, 220);
        }

        private void LoadRoleFunc()
        {
            CtrAdmin ctrA = new CtrAdmin();
            var funcs = ctrA.GetFunctionInRole(roleId);            
            lstPermission.Groups.Clear();
            lstPermission.Items.Clear();
            List<ListViewGroup> grs = new List<ListViewGroup>(); ;
            foreach (var funcP in funcs.Items)
            {
                ListViewGroup group1 = new ListViewGroup(funcP.Info.Name);
                group1.Tag = funcP.Info.FunctionID;
                lstPermission.Groups.Add(group1);
                grs.Add(group1);
                foreach (var funcc in funcP.Items)
                {
                    ListViewItem item = new ListViewItem(funcc.Name);
                    item.Tag = funcc.FunctionID + "+" + funcc.Permission;
                    for (int i = 8; i >= 1; i /= 2)
                    {
                        ListViewItem.ListViewSubItem sub1 = new ListViewItem.ListViewSubItem();
                        if ((funcc.Permission & i) == i)
                        {
                            sub1.Text = "Có";
                            sub1.Tag = i;
                        }
                        else
                        {
                            sub1.Text = "Không";
                            sub1.Tag = 0;
                        }
                        item.SubItems.Add(sub1);
                    }
                    item.Group = group1;
                    lstPermission.Items.Add(item);
                }

            }
            lstPermission.Groups.AddRange(grs.ToArray());
        }

        private void LoadAdmin()
        {
            grvAdmin.AutoGenerateColumns = false;
            CtrAdmin ctrA = new CtrAdmin();
            var admins = ctrA.GetListAdminInRole(roleId);            
            grvAdmin.DataSource = admins.Items;            
        }

        private void contextMenuStripSetRF_Opening(object sender, CancelEventArgs e)
        {
            try
            {
                var contextMenuStrip1 = (ContextMenuStrip)sender;
                if (lstPermission.SelectedItems.Count != 1)
                {
                    viewToolStripMenuItem.Enabled = false;
                    addToolStripMenuItem.Enabled = false;
                    editToolStripMenuItem.Enabled = false;
                    deleteToolStripMenuItem.Enabled = false;
                    viewToolStripMenuItem.Checked = false;
                    addToolStripMenuItem.Checked = false;
                    editToolStripMenuItem.Checked = false;
                    deleteToolStripMenuItem.Checked = false;
                }
                else
                {
                    ListViewItem lvItem1 = lstPermission.SelectedItems[0];
                    viewToolStripMenuItem.Enabled = true;
                    viewToolStripMenuItem.Checked=Convert.ToInt32(lvItem1.SubItems[1].Tag)!=0;
                    addToolStripMenuItem.Enabled = true;
                    addToolStripMenuItem.Checked = Convert.ToInt32(lvItem1.SubItems[2].Tag) != 0;
                    editToolStripMenuItem.Enabled = true;
                    editToolStripMenuItem.Checked = Convert.ToInt32(lvItem1.SubItems[3].Tag) != 0;
                    deleteToolStripMenuItem.Enabled = true;
                    deleteToolStripMenuItem.Checked = Convert.ToInt32(lvItem1.SubItems[4].Tag) != 0;
                }
            }
            catch
            {
                viewToolStripMenuItem.Enabled = false;
                addToolStripMenuItem.Enabled = false;
                editToolStripMenuItem.Enabled = false;
                deleteToolStripMenuItem.Enabled = false;
                viewToolStripMenuItem.Checked = false;
                addToolStripMenuItem.Checked = false;
                editToolStripMenuItem.Checked = false;
                deleteToolStripMenuItem.Checked = false;
            }
        }

        private void viewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem lvItem1 = lstPermission.SelectedItems[0];
            int functionID=Convert.ToInt32(lvItem1.Tag.ToString().Split('+')[0]);
            int per=  Convert.ToInt32(lvItem1.Tag.ToString().Split('+')[1]);
            int index = lvItem1.Index;
            CtrAdmin ctra = new CtrAdmin();
            string des = "";
            if (viewToolStripMenuItem.Checked)
            {
                per = 0;
                des = "Bỏ quyền xem \"" + lvItem1.Text + "\" với nhóm \"" + treeRole.SelectedNode.Text + "\"";
            }
            else
            {
                per = per | 8;
                des = "Cấp quyền xem \"" + lvItem1.Text + "\" cho nhóm \"" + treeRole.SelectedNode.Text + "\"";
            }
            if(ctra.UpdateRoleFunction(roleId, functionID, per)>0)
            {
                ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 2, des,"Phân quyền");
                LoadRoleFunc();
                lstPermission.Items[index].Selected = true;
            }
                
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem lvItem1 = lstPermission.SelectedItems[0];
            int functionID = Convert.ToInt32(lvItem1.Tag.ToString().Split('+')[0]);
            int per = Convert.ToInt32(lvItem1.Tag.ToString().Split('+')[1]);
            int index = lvItem1.Index;
            CtrAdmin ctra = new CtrAdmin();
            string des = "";
            if (addToolStripMenuItem.Checked)
            {
                per = per & 11;
                des = "Bỏ quyền thêm mới \"" + lvItem1.Text + "\" với nhóm \"" + treeRole.SelectedNode.Text + "\"";
            }
            else
            {
                per = per | 4;
                per = per | 8;
                des = "Cấp quyền thêm mới \"" + lvItem1.Text + "\" cho nhóm \"" + treeRole.SelectedNode.Text + "\"";
            }
            if (ctra.UpdateRoleFunction(roleId, functionID, per) > 0)
            {
                
                ctra.LogInsert(Program.UserEntity.UserID,Program.UserEntity.UserName, 2, des,"Phân quyền");
                LoadRoleFunc();
                lstPermission.Items[index].Selected = true;
            }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem lvItem1 = lstPermission.SelectedItems[0];
            int functionID = Convert.ToInt32(lvItem1.Tag.ToString().Split('+')[0]);
            int per = Convert.ToInt32(lvItem1.Tag.ToString().Split('+')[1]);
            int index = lvItem1.Index;
            CtrAdmin ctra = new CtrAdmin();
            string des = "";
            if (editToolStripMenuItem.Checked)
            {
                per = per & 13;
                des = "Bỏ quyền sửa \"" + lvItem1.Text + "\" với nhóm \"" + treeRole.SelectedNode.Text + "\"";
            }
            else
            {
                per = per | 2;
                per = per | 8;
                des = "Cấp quyền sửa \"" + lvItem1.Text + "\" cho nhóm \"" + treeRole.SelectedNode.Text + "\"";
            }
            if (ctra.UpdateRoleFunction(roleId, functionID, per) > 0)
            {
                ctra.LogInsert(Program.UserEntity.UserID,Program.UserEntity.UserName, 2, des,"Phân quyền");
                LoadRoleFunc();
                lstPermission.Items[index].Selected = true;
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListViewItem lvItem1 = lstPermission.SelectedItems[0];
            int functionID = Convert.ToInt32(lvItem1.Tag.ToString().Split('+')[0]);
            int per = Convert.ToInt32(lvItem1.Tag.ToString().Split('+')[1]);
            int index = lvItem1.Index;
            CtrAdmin ctra = new CtrAdmin();
            string des = "";
            if (deleteToolStripMenuItem.Checked)
            {
                per = per & 14;
                des = "Bỏ quyền xóa \"" + lvItem1.Text + "\" với nhóm \"" + treeRole.SelectedNode.Text + "\"";
            }
            else
            {
                per = per | 1;
                per = per | 8;
                des = "Cấp quyền xóa \"" + lvItem1.Text + "\" cho nhóm \"" + treeRole.SelectedNode.Text + "\"";
            }
            if (ctra.UpdateRoleFunction(roleId, functionID, per) > 0)
            {
                ctra.LogInsert(Program.UserEntity.UserID,Program.UserEntity.UserName, 2, des,"Phân quyền");
                LoadRoleFunc();
                lstPermission.Items[index].Selected = true;
            }
        }

        private void treeRole_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (treeRole.SelectedNode != null)
                treeRole.SelectedNode.BackColor = Color.Transparent;
        }

        private void btnAddAdmin_Click(object sender, EventArgs e)
        {
            popupAddAdminInRole popAdd = new popupAddAdminInRole(this, roleId);
            popAdd.ShowDialog();
        }

        public void addAdminsIntoRole(string adminIds)
        {
            if (adminIds == "")
                return;
            CtrAdmin ctra = new CtrAdmin();
            string userNames="";
            if (ctra.InsertPermissionMultiAdminToRole(adminIds, roleId, ref userNames) > 0)
            {
                ctra.LogInsert(Program.UserEntity.UserID,Program.UserEntity.UserName, 1, "Thêm \"" + userNames + "\" vào nhóm quyền \"" + treeRole.SelectedNode.Text + "\"","Phân quyền");
                LoadAdmin();
            }
        }

        private void btnDeleteAdmin_Click(object sender, EventArgs e)
        {
            if (grvAdmin.SelectedRows.Count <= 0)
                return;
            int permissionID = Convert.ToInt32(grvAdmin.SelectedRows[0].Cells["PermissionID"].Value);
            CtrAdmin ctra = new CtrAdmin();
            string uName = "";
            if (ctra.DeletePermission(permissionID,ref uName) > 0)
            {
                ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 3, "Xóa \"" + uName + "\" khỏi nhóm quyền \"" + treeRole.SelectedNode.Text + "\"","Phân quyền");
                LoadAdmin();
            }
        }
    }
}
